﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DVSProject.Models;

using System.Data;
using DVSProject.Lib;
using System.Web.UI;
using System.Text;


namespace DVSProject.Controllers
{
    public class FromMvcController : Controller
    {
        //
        // GET: /FromMvc/

        public ActionResult Index()
        {
            return View();
        }
        //Used for showing simple report
        public void ShowSimple()
        {

            using (ReportClass rptH = new ReportClass())
            {

                rptH.FileName = Server.MapPath("~/") + "//Rpts//rptSumMeterByDate.rpt";
                rptH.Load();
                rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
            }
        }


        /// <summary>
        /// Generating Report Data
        /// </summary>
        /// <returns></returns>
        private List<Student> GetStudents()
        {


            return new List<Student>() { 
            new Student(){StudentID=1,StudentName="Hasibul"},
            new Student(){StudentID=2,StudentName="Tst"}
            };
        }


        private DataTable GetSummaryByRangeDate(string ProductName, string start_date, string end_date)
        {
            ReportRepo _ReportRepo = new ReportRepo();
            DataTable  SummaryMeterList = _ReportRepo.GetSummaryMeterByRangeDate(ProductName, start_date, end_date);
            return SummaryMeterList;
        }

        private DataTable GetComplaintByRangeDate( string start_date, string end_date)
        {
            ReportRepo _ReportRepo = new ReportRepo();
            DataTable ComplaintList = _ReportRepo.GetComplainByRangeDate( start_date, end_date);
            return ComplaintList;
        }
        /// <summary>
        /// This is used for showing Generic Report(with data and report parameter) in a same window
        /// </summary>
        /// <param name="txtFromDate">This will be passed to Report for showing from Date</param>
        /// <param name="txtToDate">This will be passed to Report for showing to Date</param>
        /// <returns></returns>
        public ActionResult ShowGeneric(string txtFromDate, string txtToDate)
        {

            this.HttpContext.Session["ReportName"] = "generic.rpt";
            this.HttpContext.Session["rptFromDate"] = txtFromDate;
            this.HttpContext.Session["rptToDate"] = txtToDate;
            this.HttpContext.Session["rptSource"] = GetStudents();


            return RedirectToAction("ShowGenericRpt", "FromMvc");
        }



        /// <summary>
        /// This is used for preprocess report data and next generic report called from java script block
        /// </summary>
        /// <param name="FromDate"></param>
        /// <param name="ToDate"></param>
        [HttpPost]
        public void ShowGenericRptInNewWin(string FromDate, string ToDate)
        {
            this.HttpContext.Session["ReportName"] = "generic.rpt";
            this.HttpContext.Session["rptFromDate"] = "10/01/2013"; //FromDate;
            this.HttpContext.Session["rptToDate"] = "11/02/2013"; //ToDate;
            this.HttpContext.Session["rptSource"] = GetStudents();

        }
        public ActionResult IndexLoadDefault()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ReportModels ReportModel = new ReportModels();

            ReportModel.start_date = DateTime.Now;
            ReportModel.end_date = DateTime.Now;
            
            ReportRepo _ReportRepo = new ReportRepo();
            IEnumerable<ProductTypeModels> _ProductTypes = _ReportRepo.GetProductType();
            string selectedId = "เครื่องล้างจาน";
            IEnumerable<SelectListItem> selectlist = from s in _ProductTypes
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.NameThValue == selectedId),
                                                         Text = s.NameThText,
                                                         Value = s.NameThValue.ToString()
                                                     };

            //SF  _SF = new SF();

            
            IEnumerable<MonthModels> _Months = _ReportRepo.GetMonthAll();
            string selectedMachinId = "มกราคม";
            IEnumerable<SelectListItem> MonthShowSelect = from s in _Months
                                                          select new SelectListItem
                                                          {
                                                              Selected = (s.month_name == selectedMachinId),
                                                              Text = s.month_name,
                                                              Value = s.month_name.ToString()
                                                          };

            ReportModel.ProductSelect = selectlist;
            ReportModel.MonthShowSelect = MonthShowSelect;
            return View(ReportModel);
        }

        [HttpPost]
        public void IndexLoadDefault(FormCollection collection)
        {
            //if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            //try
            //{
            string _product = collection["product"];
            string _set_month = collection["set_month"];
            string _start_date = collection["start_date"];
            string _end_date = collection["end_date"];
            this.HttpContext.Session["ReportName"] = "rptSumMeterByDate.rpt";
            this.HttpContext.Session["rptProduct"] = _product;
            this.HttpContext.Session["rptMonthShow"] = _set_month;
            this.HttpContext.Session["rptFromDate"] = _start_date;
            this.HttpContext.Session["rptToDate"] = _end_date;


            _start_date = ConvertMMddYYYY(_start_date);
            _end_date = ConvertMMddYYYY(_end_date);
            this.HttpContext.Session["rptSource"] = GetSummaryByRangeDate(_product, _start_date, _end_date);


            Response.Redirect("~/AspNetForms/aspnetsimple.aspx");

        }
        //new ++++++++++++++++++++++++++++++
        [HttpPost]
        public ActionResult IndexLoadDefaultGrid(FormCollection collection)
        {
            //if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            //try
            //{
            string _product = collection["product"];
            string _set_month = collection["set_month"];
            string _start_date = collection["start_date"];
            string _end_date = collection["end_date"];

            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ReportModels ReportModel = new ReportModels();

            ReportModel.start_date = DateTime.Now;
            ReportModel.end_date = DateTime.Now;

            ReportRepo _ReportRepo = new ReportRepo();
            IEnumerable<ProductTypeModels> _ProductTypes = _ReportRepo.GetProductType();
            string selectedId = _product;// "เครื่องล้างจาน";
            IEnumerable<SelectListItem> selectlist = from s in _ProductTypes
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.NameThValue == selectedId),
                                                         Text = s.NameThText,
                                                         Value = s.NameThValue.ToString()
                                                     };

            //SF  _SF = new SF();


            IEnumerable<MonthModels> _Months = _ReportRepo.GetMonthAll();
            string selectedMachinId = _set_month;// "มกราคม";
            IEnumerable<SelectListItem> MonthShowSelect = from s in _Months
                                                          select new SelectListItem
                                                          {
                                                              Selected = (s.month_name == selectedMachinId),
                                                              Text = s.month_name,
                                                              Value = s.month_name.ToString()
                                                          };

            ReportModel.ProductSelect = selectlist;
            ReportModel.MonthShowSelect = MonthShowSelect;


            _start_date = ConvertMMddYYYY(_start_date);
            _end_date = ConvertMMddYYYY(_end_date);

            //ReportRepo _ReportRepo = new ReportRepo();
            //ReportModels _ReportModels = new ReportModels();
            ReportModel.SummaryMeter = _ReportRepo.GetSummaryMeterByRangeDateDtl(_product, _start_date, _end_date);

            ReportModel.start_date = Convert.ToDateTime(collection["start_date"]);
            ReportModel.end_date = Convert.ToDateTime(collection["end_date"]);
            return View(ReportModel);

            //this.HttpContext.Session["rptSource"] = GetSummaryByRangeDate(_product, _start_date, _end_date);


            //Response.Redirect("~/AspNetForms/aspnetsimple.aspx");

        }

        //new
        [HttpPost]
        public void ExportPDF(FormCollection collection)
        {
        //    //if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
        //    //try
        //    //{
            string _product = collection["product"];
            string _set_month = collection["set_month"];
            string _start_date = collection["start_date"];
            string _end_date = collection["end_date"];
        //        this.HttpContext.Session["ReportName"] = "rptSumMeterByDate.rpt";
        //        this.HttpContext.Session["rptProduct"] = _product;
        //        this.HttpContext.Session["rptMonthShow"] = _set_month;
        //        this.HttpContext.Session["rptFromDate"] = _start_date;
        //        this.HttpContext.Session["rptToDate"] = _end_date;


        //        _start_date = ConvertMMddYYYY(_start_date);
        //        _end_date = ConvertMMddYYYY(_end_date);
        //        this.HttpContext.Session["rptSource"] = GetSummaryByRangeDate(_product, _start_date, _end_date);


        //        Response.Redirect("~/AspNetForms/aspnetsimple.aspx");

        }
        //new
        [HttpPost]
        public void ExportExcel(FormCollection collection)
        {
        //    //if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
        //    //try
        //    //{
            string _product = collection["product"];
            string _set_month = collection["set_month"];
            string _start_date = collection["start_date"];
            string _end_date = collection["end_date"];
        //        this.HttpContext.Session["ReportName"] = "rptSumMeterByDate.rpt";
        //        this.HttpContext.Session["rptProduct"] = _product;
        //        this.HttpContext.Session["rptMonthShow"] = _set_month;
        //        this.HttpContext.Session["rptFromDate"] = _start_date;
        //        this.HttpContext.Session["rptToDate"] = _end_date;


        //        _start_date = ConvertMMddYYYY(_start_date);
        //        _end_date = ConvertMMddYYYY(_end_date);
        //        this.HttpContext.Session["rptSource"] = GetSummaryByRangeDate(_product, _start_date, _end_date);


        //        Response.Redirect("~/AspNetForms/aspnetsimple.aspx");

       }


        private string ConvertMMddYYYY(string date)
        {
            date = date.Substring(0, 10);
            string[] splitdate = date.Split('-');
            //Int32 y = Convert.ToInt32(splitdate[2]);
            date = splitdate[1] + "/" + splitdate[0] + "/" + splitdate[2];// Convert.ToString(2);
            return date;
        }

        public ActionResult IndexComplaint()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ReportModels ReportModel = new ReportModels();

            ReportModel.start_date = DateTime.Now;
            ReportModel.end_date = DateTime.Now;

            ReportRepo _ReportRepo = new ReportRepo();
            //IEnumerable<ProductTypeModels> _ProductTypes = _ReportRepo.GetProductType();
            //string selectedId = "เครื่องล้างจาน";
            //IEnumerable<SelectListItem> selectlist = from s in _ProductTypes
            //                                         select new SelectListItem
            //                                         {
            //                                             Selected = (s.NameThValue == selectedId),
            //                                             Text = s.NameThText,
            //                                             Value = s.NameThValue.ToString()
            //                                         };

            ////SF  _SF = new SF();


            IEnumerable<MonthModels> _Months = _ReportRepo.GetMonthAll();
            string selectedMachinId = "มกราคม";
            IEnumerable<SelectListItem> MonthShowSelect = from s in _Months
                                                          select new SelectListItem
                                                          {
                                                              Selected = (s.month_name == selectedMachinId),
                                                              Text = s.month_name,
                                                              Value = s.month_name.ToString()
                                                          };

           // ReportModel.ProductSelect = selectlist;
            ReportModel.MonthShowSelect = MonthShowSelect;
            return View(ReportModel);
        }
        
        [HttpPost]
        public void IndexComplaint(FormCollection collection)
        {

            string _set_month = collection["set_month"];
            string _start_date = collection["start_date"];
            string _end_date = collection["end_date"];
            this.HttpContext.Session["ReportName"] = "rptComplain.rpt";
            this.HttpContext.Session["rptMonthShow"] = _set_month;
            this.HttpContext.Session["rptFromDate"] = _start_date;
            this.HttpContext.Session["rptToDate"] = _end_date;


            _start_date = ConvertMMddYYYY(_start_date);
            _end_date = ConvertMMddYYYY(_end_date);
            
            DataTable dtSource  = GetComplaintByRangeDate( _start_date, _end_date);
            if (dtSource.Rows.Count > 0)
            {
                this.HttpContext.Session["rptSource"] = GetComplaintByRangeDate(_start_date, _end_date);
               // Response.Redirect("~/AspNetForms/aspnetsimplec.aspx");
            }
            Response.Redirect("~/AspNetForms/aspnetsimplec.aspx");

        }



        //public ActionResult IndexVissit()
        //{
        //    if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
        //    ReportModels ReportModel = new ReportModels();

        //    ReportModel.start_date = DateTime.Now;
        //    ReportModel.end_date = DateTime.Now;

        //    ReportRepo _ReportRepo = new ReportRepo();
        //    //IEnumerable<ProductTypeModels> _ProductTypes = _ReportRepo.GetProductType();
        //    //string selectedId = "เครื่องล้างจาน";
        //    //IEnumerable<SelectListItem> selectlist = from s in _ProductTypes
        //    //                                         select new SelectListItem
        //    //                                         {
        //    //                                             Selected = (s.NameThValue == selectedId),
        //    //                                             Text = s.NameThText,
        //    //                                             Value = s.NameThValue.ToString()
        //    //                                         };

        //    ////SF  _SF = new SF();


        //    IEnumerable<MonthModels> _Months = _ReportRepo.GetMonthAll();
        //    string selectedMachinId = "มกราคม";
        //    IEnumerable<SelectListItem> MonthShowSelect = from s in _Months
        //                                                  select new SelectListItem
        //                                                  {
        //                                                      Selected = (s.month_name == selectedMachinId),
        //                                                      Text = s.month_name,
        //                                                      Value = s.month_name.ToString()
        //                                                  };

        //    // ReportModel.ProductSelect = selectlist;
        //    ReportModel.MonthShowSelect = MonthShowSelect;
        //    return View(ReportModel);
        //}

        //[HttpPost]
        //public void IndexVissit(FormCollection collection)
        //{

        //    string _set_month = collection["set_month"];
        //    string _start_date = collection["start_date"];
        //    string _end_date = collection["end_date"];
        //    this.HttpContext.Session["ReportName"] = "rptComplain.rpt";
        //    this.HttpContext.Session["rptMonthShow"] = _set_month;
        //    this.HttpContext.Session["rptFromDate"] = _start_date;
        //    this.HttpContext.Session["rptToDate"] = _end_date;


        //    _start_date = ConvertMMddYYYY(_start_date);
        //    _end_date = ConvertMMddYYYY(_end_date);
        //    this.HttpContext.Session["rptSource"] = GetComplaintByRangeDate(_start_date, _end_date);


        //    Response.Redirect("~/AspNetForms/aspnetsimplec.aspx");

        //}
    }
}
